const { request } = require('../../services/request')
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        photoPath: '',
        menuId: '',
        userId: '',
        answeUserId: '',
        cameraPosition: 'front',
        scrollHeight: '',
        tonguePhotoList: [], //舌面 舌底照片
        cameraStatus: 0, // 0拍照 1预览 2查看示例
        currnetImg: '',
        currentStep: 0, // 0舌面 1 舌底
        id: '',
        sampleImageUrl: [],
        showGif: false,
        showExp: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const { menuId, userId, answeUserId, id } = options
        this.setData({
            menuId,
            userId,
            answeUserId,
            id,
            scrollHeight: app.globalData.safeHeight * app.globalData.logicPixelRatio - 204 - 88,
        })

    },
    showSample() {
        console.log('show sample :>> ')
        this.setData({ showExp: true })
    },
    handelHideExp() {
        this.setData({ showExp: false })
    },
    handleChangeCameraPosition() {
        this.setData({
            cameraPosition: this.data.cameraPosition === 'front' ? 'back' : 'front',
        })
    },
    handleBackToCamera() {
        this.setData({ cameraStatus: 0 })
    },
    requestImageCheck(filePath, cb) {
        let { userId, answeUserId, currentStep, tonguePhotoList } = this.data
        let baseUrl = 'http://chunfengai.top:28081'
            // let baseUrl = 'http://127.0.0.1:8080'
        wx.uploadFile({
                filePath: filePath,
                name: 'file',
                header: {
                    'Authorization': `Bearer ${wx.getStorageSync('token')}`,
                },
                url: `${baseUrl}/applet/dialog/pic/checkpic?userId=${userId}&answeUserId=${answeUserId}&type=${currentStep}`,
                formData: {},
                success: (res) => {
                    let data = JSON.parse(res.data).data
                    console.log(data, res)
                    if (data && data.success === '1') {
                        cb(data.uploadFileName)
                    } else {
                        wx.showToast({
                            title: data.code,
                        })
                    }
                },
                fail: (err) => {
                    console.log(err)
                }
            })
            // 如果用base64方式上传就需要走下面的逻辑
            // let that = this
            // wx.getFileSystemManager().readFile({
            //   filePath: filePath,
            //   encoding: "base64",
            //   success: function (res) {
            //       let base64 = '' + res.data
            //       request('POST', `/applet/dialog/pic/checkpic`, {
            //         userId,
            //         answeUserId,
            //         type: currentStep,
            //         file:base64
            //       }).then(res => {
            //         cb(base64)
            //       })
            //   }
            // })
    },
    handleUpload() {
        let { userId, answeUserId, tonguePhotoList } = this.data
        let that = this
        wx.chooseMedia({
                mediaType: ['image'],
                count: 1,
                sizeType: "original",
                success: (res) => {
                    let filePath = res.tempFiles[0].tempFilePath
                    let successFn = (id) => {
                        tonguePhotoList.push({
                            path: filePath,
                            status: 0, // 待确认 1确认
                            id
                        })
                        that.setData({
                                tonguePhotoList: tonguePhotoList,
                                cameraStatus: 1,
                                currnetImg: filePath,
                            })
                            // wx.saveImageToPhotosAlbum({
                            //   filePath: res.tempImagePath,
                            //   success:(res)=>{
                            //   }
                            // })
                    }
                    this.requestImageCheck(filePath, successFn)
                },
                fail: (err) => {
                    console.log(err)
                }
            })
            // wx.navigateTo({
            //   url: '/pages/image-upload/index',
            // })
    },
    takePhoto() {
        wx.vibrateShort()
        let photoList = this.data.tonguePhotoList
        let that = this
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                let successFn = (id) => {
                    photoList.push({
                        path: res.tempImagePath,
                        status: 0, // 待确认 1确认
                        id,
                    })
                    that.setData({
                        tonguePhotoList: photoList,
                        cameraStatus: 1,
                        currnetImg: res.tempImagePath,
                    })
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempImagePath,
                        success: (res) => {},
                    })
                }
                this.requestImageCheck(res.tempImagePath, successFn)
            },
            fail: (err) => {
                console.log(err)
            },
        })
    },
    handleReTake() {
        let arr = this.data.tonguePhotoList
        arr.pop()
        this.setData({
            tonguePhotoList: arr,
            currnetImg: '',
            cameraStatus: 0,
        })
    },
    handleNext() {
        const {
            currentStep,
            tonguePhotoList,
            userId,
            answeUserId,
            answeUserName,
            menuId,
            id
        } = this.data
        if (currentStep === 0) {
            this.setData({
                currentStep: 1,
                currnetImg: '',
                cameraStatus: 0,
            })
        } else {
            this.setData({ showGif: true })
            setTimeout(() => {
                this.setData({ showGif: false })
                wx.redirectTo({
                    url: `/pages/channel/index?id=${id}&image1path=${
            tonguePhotoList[0].path
          }&image1id=${tonguePhotoList[0].id}&image2path=${
            tonguePhotoList[1] ? tonguePhotoList[1].path : ''
          }&image2id=${
            tonguePhotoList[1] ? tonguePhotoList[1].id : ''
          }&userId=${userId}&menuId=${menuId}&answeUserId=${answeUserId}`,
                })
            }, 5000)
        }
    },
    handleJump() {
        const {
            currentStep,
            tonguePhotoList,
            userId,
            answeUserId,
            answeUserName,
            menuId,
            id
        } = this.data
        this.setData({ showGif: true })
        setTimeout(() => {
            this.setData({ showGif: false })
            wx.redirectTo({
                url: `/pages/channel/index?id=${id}&image1path=${
          tonguePhotoList[0].path
        }&image1id=${tonguePhotoList[0].id}&image2path=${
          tonguePhotoList[1] ? tonguePhotoList[1].path : ''
        }&image2id=${
          tonguePhotoList[1] ? tonguePhotoList[1].id : ''
        }&userId=${userId}&menuId=${menuId}&answeUserId=${answeUserId}`,
            })
        }, 2000)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})